public class ControlPanel {
    private static final int PASSWORD_LENGTH = 4;
    private static final int MAX_ATTEMPT = 3;
    private static final int LOCK_TIME_SECONDS = 120;

    public State getState() {
        return state;
    }

    private enum State{
        IDEL,READING_PASSWORD,LOCKED,FUNCTION_SELECTION
    }
    private State state;
    private String password;
    private int attempts;
    private long locktime;

    public ControlPanel(){
        state = State.IDEL;
        password = "1234";
        attempts = 0;
        locktime = 0;
    }
    public void processKeyPress(char key){
        switch (state){
            case IDEL:
                if(key=='#'){
                    state = State.READING_PASSWORD;
                }
                break;

            case READING_PASSWORD:
                if(password.length()<PASSWORD_LENGTH){
                    password+=key;
                }

                if(password.length()==PASSWORD_LENGTH){
                    validatePassword();
                }
                break;

            case LOCKED:
                long currentTime = System.currentTimeMillis()/1000;
                if(currentTime-locktime>=LOCK_TIME_SECONDS){
                    state = State.IDEL;
                    password = "";
                    attempts = 0;
                }
                break;

            case FUNCTION_SELECTION:
                break;
        }
    }
    private void validatePassword(){
        if(password.equals("1234")){
            state = State.FUNCTION_SELECTION;
        }
        else{
            attempts++;
            if(attempts>=MAX_ATTEMPT){
                state = State.LOCKED;
                locktime = System.currentTimeMillis()/1000;
            }
        }
        password = "";
    }
}